package com.devcon5.StudentPlanner;

import baseclasses.Assignment;
import baseclasses.Grade;
import baseclasses.Reminder;
import database.AssignmentDAO;
import database.GradeDAO;
import database.ReminderDAO;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class AssignmentView extends Activity {

	private TextView name, description, duedate, course, gradeTextView, reminderTextView, statusTextView;
	private Assignment assignment;
	private Button addGradeButton, addReminderButton, deleteGradeButton, deleteReminderButton, completedButton;
	private Context context = this;
	private int id;
	private Grade grade;
	private Reminder reminder;
	
	//Special assignment view that activates when "View" is clicked in the context menu generated for an assignment
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.singleassignmentview);

		//Unpack the assignment id from the extras passed via intent
		id = getIntent().getExtras().getInt( Assignment.ASSIGNMENT_ID );
		assignment = AssignmentDAO.getAssignmentById(context, id);	//retrieve the associated assignment from the db

		//initialize variables
		name = (TextView)findViewById(R.id.assignmentview_name);
		description = (TextView)findViewById(R.id.assignmentview_description);
		duedate = (TextView)findViewById(R.id.assignmentview_duedate);
		course = (TextView)findViewById(R.id.assignmentview_course);
		gradeTextView = (TextView)findViewById(R.id.assignmentview_grade);
		addGradeButton = (Button)findViewById(R.id.assignmentview_addgradebutton);
		reminderTextView = (TextView)findViewById(R.id.assignmentview_reminder);
		name.setText(assignment.getName());
		description.setText(assignment.getDescription());
		duedate.setText(assignment.getDueDateString("MMM dd yyyy hh:mm a"));
		course.setText(assignment.getCourseName(context));
		addReminderButton = (Button)findViewById(R.id.assignmentview_reminderbutton);
		deleteGradeButton = (Button)findViewById(R.id.assignmentview_deletegradebutton);
		deleteReminderButton = (Button)findViewById(R.id.assignmentview_deletereminderbutton);
		statusTextView = (TextView)findViewById(R.id.assignmentview_status);
		completedButton = (Button)findViewById(R.id.assignmentview_completed_button);

		//get grade and reminder associated with the assignment from db
		grade = GradeDAO.getGradeByAssignmentId(context, id);
		reminder = ReminderDAO.getReminderByAssignment(context, assignment);

		
		if( reminder!=null )	//if there's a reminder associated, display it, 
								//change the addReminderButton to "Edit Reminder" button
								//and show the deleteReminderButton
		{
			reminderTextView.setText("Reminder: " 
					+ reminder.getInteger()+" "+reminder.getUnits()+" before due");
			addReminderButton.setText("Edit Reminder");
			deleteReminderButton.setVisibility(View.VISIBLE);
		}
		else
		{	//otherwise, just show that there's no reminder; addReminderButton only
			reminderTextView.setText("Reminder: Not Set");
			addReminderButton.setText("Add Reminder");
			deleteReminderButton.setVisibility(View.GONE);
		}

		addGradeButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				if(grade==null){	//if there's no grade associated, go to the addGrade class from here
					Intent addGrade = new Intent(context, AddGrade.class);
					//include the assignment id to send to the addgrade activity
					addGrade.putExtra( Assignment.ASSIGNMENT_ID, assignment.getId());
					startActivity(addGrade);
				}
				else{	//if there is a grade, go to the editgrade class from here
					
					Intent editGrade = new Intent(context, EditGrade.class);
					editGrade.putExtra( Grade.GRADE_ID, grade.getId());
					startActivity(editGrade);
				}


			}
		});

		addReminderButton.setOnClickListener(new View.OnClickListener() {
	
			public void onClick(View v) {
				//Go to the editassignment class from here with an 
				//intent with extras: AssignmentID & true value for addreminder var
				//will pop up the reminder tab in the edit assignment activity
				Intent editIntent = new Intent(context, EditAssignment.class);
				editIntent.putExtra( Assignment.ASSIGNMENT_ID, assignment.getId() );
				editIntent.putExtra(EditAssignment.ADD_REMINDER, true);
				startActivity(editIntent);
				
			}
		});
		
		
		deleteGradeButton.setOnClickListener(new View.OnClickListener() {
		
			public void onClick(View v) {
				//display dialog that confirms delete, then delete the grade and update the display,
				//give the option to add a new grade, remove the deleteGradeButton
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setTitle("Delete?");
				builder.setIcon(android.R.drawable.ic_delete);
				builder.setMessage("Are you sure you want to delete the grade for this assignment?");
				builder.setPositiveButton("Delete", new OnClickListener() {
					
			
					public void onClick(DialogInterface dialog, int which) {
						
						if(GradeDAO.deleteGrade(context, grade)!=-1){
							grade = null;
							addGradeButton.setText("Add Grade");
							deleteGradeButton.setVisibility(View.GONE);
							gradeTextView.setText("Grade: Not Set");
							
						}
						
					}
				});
				builder.setNegativeButton("Cancel", null);
				AlertDialog alert = builder.create();
				alert.show();
	
			}
		});
		
		deleteReminderButton.setOnClickListener(new View.OnClickListener() {
		
			public void onClick(View v) {
				//display dialog that confirms delete, then delete the reminder and update the display,
				//give the option to add a new reminder, remove the deleteGradeButton
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setTitle("Delete?");
				builder.setIcon(android.R.drawable.ic_delete);
				builder.setMessage("Are you sure you want to delete the reminder for this assignment?");
				builder.setPositiveButton("Delete", new OnClickListener() {
					
			
					public void onClick(DialogInterface dialog, int which) {
						
						if(ReminderDAO.deleteReminder(context, reminder)!=-1){
							reminder = null;
							addReminderButton.setText("Add Reminder");
							deleteReminderButton.setVisibility(View.GONE);
							reminderTextView.setText("Reminder: Not Set");
							
						}
						
						
					}
				});
				builder.setNegativeButton("Cancel", null);
				AlertDialog alert = builder.create();
				alert.show();
	
			}
		});
		
		completedButton.setOnClickListener( new View.OnClickListener()
		{
		
			public void onClick(View v)
			{
				if( !assignment.isCompleted() )	//if it's not already complete, mark it as such
				{
					assignment.setCompleted( true );
				}
				else
				{
					assignment.setCompleted( false );	//other choice is to mark it incomplete
				}
				
				AssignmentDAO.markAssigment(context, assignment);	//translate the change to the db
				updateOnCompleteChange();	//update the display
			}
		});

		if( grade!=null )	//if there's already a grade set
		{
			gradeTextView.setText("Grade: "+ grade.getGradeString(context));	//display it
			addGradeButton.setText("Edit Grade");	//and change the addgrade text to edit grade 
			deleteGradeButton.setVisibility(View.VISIBLE);	//and show the delete grade button
		}
		else
		{	//otherwise set addgradebutton text to add grade & give no delete option
			gradeTextView.setText("Grade: Not Set");
			addGradeButton.setText("Add Grade");
			deleteGradeButton.setVisibility(View.GONE);
		}
		
		updateOnCompleteChange(); //update the disply
		

	}
	
	public void updateOnCompleteChange()
	{
		if( !assignment.isCompleted() )	//if assignment isn't marked completed
		{
			statusTextView.setText( "Status: Incomplete" );
			completedButton.setText( "Mark As Completed" );
		}
		else
		{
			statusTextView.setText( "Status: Completed" );
			completedButton.setText( "Mark As Incomplete" );
		}
	}

	
	//---------------------------------------------------------
	//
	//						MENU METHODS
	//
	//---------------------------------------------------------
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.singleassignmentviewmenu, menu); //instantiate xml viewmenu file
		return true;
	}

	//Handle menu item selection
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case R.id.edit:
			//to edit assignment, pass id to the editassignment class
			Intent editIntent = new Intent(context, EditAssignment.class);
			editIntent.putExtra( Assignment.ASSIGNMENT_ID, assignment.getId() );
			startActivity(editIntent);

			return true;

		case R.id.delete:
			
			//display delete warning on delete menu option
			AlertDialog.Builder builder2 = new AlertDialog.Builder(context);
			builder2.setTitle("Delete?");
			builder2.setIcon(android.R.drawable.ic_delete);
			builder2.setMessage("Are you sure you want to delete "+assignment.getName() +"?" +
			"\n\nThis will not affect any grades added for this assignment");
			//clear your assignments list view but don't get rid of those grades
			builder2.setPositiveButton("Delete", new OnClickListener() {

		
				public void onClick(DialogInterface dialog, int which) {
					if(AssignmentDAO.deleteAssignment(context, assignment)<=0){
						Toast.makeText(context, "Error deleting assignment from database", Toast.LENGTH_LONG).show();
					}
					
					onBackPressed();
				}
			});
			builder2.setNegativeButton("Cancel", null);
			AlertDialog alert = builder2.create();
			alert.show();

			return true;
		}
		return false;
	}

	
	@Override
	public void onResume(){ 
		super.onResume();
		
		//if the activity is pushed to the back and brought back to the front
		//retrieve all associated assignment descriptors, its course, grade, and reminders,
		//and set all of the buttons
		//to the correct text for the situation regarding no grades & no reminders.
		assignment = AssignmentDAO.getAssignmentById(context, id); 

		name.setText(assignment.getName());
		description.setText(assignment.getDescription());
		duedate.setText(assignment.getDueDateString("EEE MMMM dd yyyy hh:mm a"));

		course.setText(assignment.getCourseName(context));

		grade = GradeDAO.getGradeByAssignmentId(context, id);

		if(grade!=null){
			gradeTextView.setText("Grade: "+ grade.getGradeString(context));
			addGradeButton.setText("Edit Grade");
			deleteGradeButton.setVisibility(View.VISIBLE);
		}
		else{
			gradeTextView.setText("Grade: Not Set");
			addGradeButton.setText("Add Grade");
			deleteGradeButton.setVisibility(View.GONE);
		}

		reminder = ReminderDAO.getReminderByAssignment(context, assignment);
		if(reminder!=null){
			reminderTextView.setText("Reminder: " 
					+ reminder.getInteger()+" "+reminder.getUnits()+" before due");
			addReminderButton.setText("Edit Reminder");
			deleteReminderButton.setVisibility(View.VISIBLE);
		}
		else{
			reminderTextView.setText("Reminder: Not Set");
			addReminderButton.setText("Add Reminder");
			deleteReminderButton.setVisibility(View.GONE);
		}

	}

}
